import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { SysTenant } from '#/api/platform/systenant';

import { useAccess } from '@vben/access';

import { z } from '#/adapter/form';
import { COMMON_DICT, TENANT_DICT } from '#/api';
import { packageSelect } from '#/api/platform/systenant';
import { $t } from '#/locales';

const { hasAccessByCodes } = useAccess();

/**
 * 查询参数
 */
export function querySchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'tenantName',
      label: $t('tenant.name'),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        buttonStyle: 'solid',
        clearable: true,
        options: [
          { label: $t('common.enabled'), value: COMMON_DICT.STATUS_ENABLE },
          { label: $t('common.disabled'), value: COMMON_DICT.STATUS_DISABLE },
        ],
        optionType: 'button',
      },
      defaultValue: null,
      fieldName: 'status',
      label: $t('common.status'),
    },
  ];
}

/**
 * 获取编辑表单的字段配置。如果没有使用多语言，可以直接export一个数组常量
 */
export function formSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'tenantName',
      label: $t('tenant.name'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('tenant.name'), 2]))
        .max(20, $t('ui.formRules.maxLength', [$t('tenant.name'), 20])),
    },
    {
      component: 'Select',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          {
            label: $t('tenant.type_personal'),
            value: TENANT_DICT.TYPE_PERSONAL,
          },
          {
            label: $t('tenant.type_enterprise'),
            value: TENANT_DICT.TYPE_ENTERPRISE,
          },
        ],
        optionType: 'button',
      },
      defaultValue: TENANT_DICT.TYPE_PERSONAL,
      fieldName: 'tenantType',
      label: $t('tenant.type'),
    },
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('tenant.administrator'),
      dependencies: {
        show: (row) => {
          return !row.tenantId;
        },
        triggerFields: ['tenantId'],
      },
      rules: z
        .string()
        .min(6, $t('ui.formRules.minLength', [$t('tenant.administrator'), 6]))
        .max(
          30,
          $t('ui.formRules.maxLength', [$t('tenant.administrator'), 30]),
        ),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        defaultExpandAll: true,
        checkStrictly: true,
        allowClear: true,
        api: packageSelect,
        class: 'w-full',
        labelField: 'label',
        valueField: 'id',
      },
      fieldName: 'packageId',
      label: $t('tenant.package.package'),
      rules: z
        .string()
        .min(6, $t('ui.formRules.minLength', [$t('tenant.package.package'), 6]))
        .max(
          30,
          $t('ui.formRules.maxLength', [$t('tenant.package.package'), 30]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'contactPhone',
      label: $t('tenant.contactPhone'),
      rules: z
        .string()
        .min(6, $t('ui.formRules.minLength', [$t('tenant.contactPhone'), 6]))
        .max(20, $t('ui.formRules.maxLength', [$t('tenant.contactPhone'), 20]))
        .nullish(),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: COMMON_DICT.STATUS_ENABLE },
          { label: $t('common.disabled'), value: COMMON_DICT.STATUS_DISABLE },
        ],
        optionType: 'button',
      },
      defaultValue: COMMON_DICT.STATUS_ENABLE,
      fieldName: 'status',
      label: $t('common.status'),
    },
    {
      component: 'InputNumber',
      fieldName: 'accountLimit',
      defaultValue: 5,
      label: $t('tenant.accountLimit'),
    },
    {
      component: 'InputNumber',
      fieldName: 'roleLimit',
      defaultValue: 5,
      label: $t('tenant.roleLimit'),
    },
    {
      component: 'InputNumber',
      fieldName: 'deptLimit',
      defaultValue: 5,
      label: $t('tenant.deptLimit'),
    },
    {
      component: 'InputNumber',
      fieldName: 'postLimit',
      defaultValue: 5,
      label: $t('tenant.postLimit'),
    },
    {
      component: 'DatePicker',
      componentProps: { valueFormat: 'x' },
      fieldName: 'expireTime',
      label: $t('tenant.expireTime'),
      rules: z
        .number()
        .min(9, $t('ui.formRules.selectRequired', [$t('tenant.expireTime')])),
    },
    {
      component: 'Input',
      componentProps: {
        type: 'textarea',
      },
      fieldName: 'remark',
      label: $t('common.remark'),
      formItemClass: 'col-span-2 md:col-span-2',
    },
  ];
}

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function columnsSchema<T>(
  onStatusChange?: (
    newStatus: string,
    row: T,
  ) => PromiseLike<boolean | undefined>,
  onActionClick?: OnActionClickFn<SysTenant>,
): VxeTableGridOptions<SysTenant>['columns'] {
  return [
    { title: '序号', type: 'seq', width: 50 },
    {
      field: 'tenantName',
      title: $t('tenant.name'),
    },
    {
      field: 'tenantType',
      title: $t('tenant.type'),
    },
    {
      field: 'packageName',
      title: $t('tenant.package.name'),
    },
    {
      field: 'balance',
      title: $t('wallet.balance'),
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: 'CellSwitch',
      },
      field: 'status',
      title: $t('common.status'),
    },
    {
      field: 'contactPhone',
      title: $t('tenant.contactPhone'),
    },
    {
      field: 'expireTime',
      title: $t('tenant.expireTime'),
    },
    {
      field: 'accountLimit',
      title: $t('tenant.accountLimit'),
    },
    {
      field: 'roleLimit',
      title: $t('tenant.roleLimit'),
    },
    {
      field: 'postLimit',
      title: $t('tenant.postLimit'),
    },
    {
      field: 'deptLimit',
      title: $t('tenant.deptLimit'),
    },
    {
      field: 'createAt',
      title: $t('common.createTime'),
    },
    {
      cellRender: {
        attrs: {
          nameField: 'tenantName',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: () => {
              return hasAccessByCodes(['sys_tenant:edit']);
            },
          },
          {
            code: 'delete',
            text: true,
            show: () => {
              return hasAccessByCodes(['sys_tenant:remove']);
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      align: 'center',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('page.action.action'),
    },
  ];
}
